﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：UsersManage.cs
  //文件功能描述：会员收货地址管理
  //创建人：赵国伟
  //生成时间：2012/03/18 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class UserAddressManage
    {

        private readonly DALUserAddress dalAddress = new DALUserAddress();

        #region "设置指定用户的某条收货地址为默认； by 赵国伟 2012-03-18"
        /// <summary>
        /// 设置指定用户的某条收货地址为默认；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userAddressID"></param>
        /// <returns></returns>
        public int SetDefault(int userId, int userAddressID)
        {
            return dalAddress.SetDefault(userId, userAddressID);
        }
        #endregion

        #region "保存收货地址； by 赵国伟 2012-03-18"
        /// <summary>
        /// 保存收货地址；
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public ReturnValue  Save(UserAddress a)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail);
            if (a == null)
            {
                ret.Message = "收货地址信息不能为空！";
            }
            else
            {
                a.LastModifyDate = DateTime.Now;
                if (a.UserAddressID <= 0)
                {
                    #region "添加的情况"
                    if (dalAddress.Add(a) > 0)
                    {
                        ret.Result = ResultType.Success;
                        ret.Message = "保存成功！";
                    }
                    else
                        ret.Message = "保存失败";
                    #endregion
                }
                else
                {
                    #region "修改的情况"
                    UserAddress oldInfo = dalAddress.GetModel(a.UserAddressID);
                    a.CreateDate = oldInfo.CreateDate;
                    if (dalAddress.Update(a) > 0)
                    {
                        ret.Result = ResultType.Success;
                        ret.Message = "保存成功！";
                    }
                    else
                        ret.Message = "保存失败";
                    #endregion
                }
            }
            if (ret.IsSucc)
            {
                if (a.IsDefault)
                {
                    SetDefault(a.UserID, a.UserAddressID); //设置是否为默认送货地址。
                }
            }
            return ret;
        }
        #endregion

        #region "查询指定用户的收货地址 by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定用户的收货地址
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<UserAddressInfo> QuerList(int userId,int sorts)
        {
            string orderby = "";
            if (sorts == 0)
                orderby = "UserAddressID asc ";
            else if (sorts == 1)
                orderby = "Isdefault desc,CreateDate desc ";

            List<UserAddress> source = dalAddress.QueryList("UserID=" + userId, orderby);
            List<UserAddressInfo> target = null;
            if (source != null)
            {
                target = new List<UserAddressInfo>();
                ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
                foreach (UserAddress s in source)
                {
                    UserAddressInfo t = new UserAddressInfo();
                    t.AddressInfo = s;
                    t.Area = bllArea.GetArea(s.AreaID);
                    target.Add(t);
                }
            }
            return target;
        }
        public List<UserAddressInfo> QuerList(int userId )
        {
            return QuerList(userId, 0);
        }
        #endregion

        #region "删除一条指定的用户地址信息；by 赵国伟 2012-03-18"
        /// <summary>
        /// 删除一条指定的用户地址信息；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userAddressID"></param>
        /// <returns></returns>
        public int Delete(int userId, int userAddressID)
        {
            return dalAddress.Delete(userId, userAddressID);
        }
        #endregion

        #region "查询一条指定的用户地址信息； by 赵国伟 2012-08-18"
        /// <summary>
        /// 查询一条指定的用户地址信息；
        /// </summary>
        /// <param name="userAddressID"></param>
        /// <returns></returns>
        public UserAddressInfo GetAddress(int userAddressID)
        {
            UserAddress s = dalAddress.GetModel(userAddressID);
            UserAddressInfo d = null;

            ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
            if (s != null)
            {
                d = new UserAddressInfo();
                d.AddressInfo = s;
                d.Area = bllArea.GetArea(s.AreaID);
            }
            return d;
        }
        #endregion



    }
}
